/*******************************************************************************
 * Copyright (c) 2008, 2009 Fabio Mauro - Alberto Terraneo.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.2
 * or any later version published by the Free Software Foundation.
 *     
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU Free Documentation License for more details.
 *     
 * You should have received a copy of the GNU Free Documentation 
 * License along with this distribution; if not, send a email to authors:
 * aterraneo <at> users.sourceforge.net
 * bigmoby09 <at> users.sourceforge.net
 *******************************************************************************/
package it.blueocean.acanto.web.platform.login;

import it.blueocean.acanto.security.SpringSecurityLoginContext;
import it.blueocean.acanto.web.platform.components.errorPages.BrowserNotSupportedErrorPage;
import it.blueocean.acanto.web.platform.components.loginForm.AUsernamePasswordSignInPanel;

import org.apache.wicket.RequestCycle;
import org.apache.wicket.RestartResponseAtInterceptPageException;
import org.apache.wicket.Session;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.protocol.http.ClientProperties;
import org.apache.wicket.protocol.http.request.WebClientInfo;
import org.apache.wicket.security.WaspSession;
import org.apache.wicket.security.hive.authentication.LoginContext;
import org.apache.wicket.security.authentication.LoginException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;

public class LoginPage extends WebPage
{
	private static final Logger log = LoggerFactory.getLogger(LoginPage.class);

	public LoginPage()
	{
	    Session session = RequestCycle.get().getSession();
        WebClientInfo clientInfo = (WebClientInfo) session.getClientInfo();
        ClientProperties clientProperties = clientInfo.getProperties();
        
        if(clientProperties.isBrowserInternetExplorer())
        {
            throw new RestartResponseAtInterceptPageException(BrowserNotSupportedErrorPage.class);            
        }
        
		// stateless so the login page will not throw a timeout exception
		// note that is only a hint we need to have stateless components on the
		// page for this to work, like a statelessform
		setStatelessHint(true);

		add(new FeedbackPanel("feedback")
		{
			private static final long serialVersionUID = 1L;

			public boolean isVisible()
			{
				return anyMessage();
			}
		});

		String panelId = "signInPanel";

		newUserPasswordSignInPanel(panelId);
		
		setVersioned(false);
	}

	protected void newUserPasswordSignInPanel(String panelId)
	{
		add(new AUsernamePasswordSignInPanel(panelId)
		{
			private static final long serialVersionUID = 1L;

			public void signIn(final String username, final String password) throws LoginException
			{
				String u = username == null ? "" : username;
				String p = password == null ? "" : password;

				log.debug("requiring AuthenticationToken...");
				
				UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(u, p);

				if (authRequest == null)
				{
					log.debug("Could not authenticate a user : [" + u + "]");
					
					throw new LoginException("Could not authenticate a user : [" + u + "]");
				}

				// authentication in swarm is handled by contexts, which are disposed after use.
				LoginContext context = new SpringSecurityLoginContext(authRequest);

				((WaspSession) getSession()).login(context);
				
				log.debug("User : [" + u + "] authenticated");
			}			
		});
	}

}
